<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form action="" method="POST">
        你的运动？<input type="checkbox" id="checkboxAllBox">全选/全不选
        <br>
        <input type="checkbox" name="items" value="排球">排球
        <input type="checkbox" name="items" value="篮球">篮球
        <input type="checkbox" name="items" value="网球">网球
        <input type="checkbox" name="items" value="足球">足球
        <br>
        <input type="button" id="checkedAllBtn" value="全选">
        <input type="button" id="checkedNoBtn" value="全不选">
        <input type="button" id="checkedRevBtn" value="反选">
        <input type="button" id="sendBtn" value="提交">
    </form>
</body>
<script>
    window.onload = function () {
        // 全选
        var checkedAllBtn = document.getElementById("checkedAllBtn");
        checkedAllBtn.onclick = function () {
            var items = document.getElementsByName("items");
            for (var i = 0; i < items.length; i++) {
                items[i].checked = true;
            }
        };
        // 全不选
        var checkedNoBtn = document.getElementById("checkedNoBtn");
        checkedNoBtn.onclick = function () {
            var items = document.getElementsByName("items");
            for (var i = 0; i < items.length; i++) {
                items[i].checked = false;
            }
        };
        // 反选
        var checkedRevBtn = document.getElementById("checkedRevBtn");
        checkedRevBtn.onclick = function () {
            var items = document.getElementsByName("items");
            for (var i = 0; i < items.length; i++) {
                items[i].checked = !items[i].checked;
            }
        };
        // 提交弹窗
        var sendBtn = document.getElementById("sendBtn");
        sendBtn.onclick = function () {
            var items = document.getElementsByName("items");
            for (var i = 0; i < items.length; i++) {
                if (items[i].checked) {
                    alert(items[i].value);
                }
            }
        };
        // 全选+全不选
        var checkboxAllBox = document.getElementById("checkboxAllBox");
        checkboxAllBox.onclick = function () {
            var items = document.getElementsByName("items");
            for (var i = 0; i < items.length; i++) {
                items[i].checked = this.checked;
            }
        };
        // 全选+全不选 被动选中
        var items = document.getElementsByName("items");
        for (var i = 0; i < items.length; i++) {
            items[i].onclick = function () {
                checkboxAllBox.checked = true;
                for (var j = 0; j < items.length; j++) {
                    if (!items[j].checked) {
                        checkboxAllBox.checked = false;
                    }
                }
            };
        }
    };
</script>

</html>